#多模态渲染模块
import re
from typing import Dict, List

class MultimodalRenderer:
    def __init__(self):
        self.css_styles = self._get_css_styles()
    
    def render(self, markdown_content: str) -> str:
        """将Markdown内容渲染为HTML"""
        html = self._markdown_to_html(markdown_content)
        return self._wrap_with_template(html)
    
    def _markdown_to_html(self, markdown: str) -> str:
        """Markdown转HTML的核心逻辑"""
        html = markdown
        
        # 处理标题
        html = re.sub(r'^### (.*$)', r'<h3>\1</h3>', html, flags=re.MULTILINE)
        html = re.sub(r'^## (.*$)', r'<h2>\1</h2>', html, flags=re.MULTILINE)
        html = re.sub(r'^# (.*$)', r'<h1>\1</h1>', html, flags=re.MULTILINE)
        
        # 处理粗体
        html = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', html)
        
        # 处理斜体
        html = re.sub(r'\*(.*?)\*', r'<em>\1</em>', html)
        
        # 处理代码块
        html = re.sub(
            r'```rust\n(.*?)\n```',
            r'<pre class="code-block rust"><code>\1</code></pre>',
            html,
            flags=re.DOTALL
        )
        html = re.sub(
            r'```(.*?)\n(.*?)\n```',
            r'<pre class="code-block \1"><code>\2</code></pre>',
            html,
            flags=re.DOTALL
        )
        
        # 处理行内代码
        html = re.sub(r'`(.*?)`', r'<code class="inline-code">\1</code>', html)
        
        # 处理列表
        html = re.sub(r'^\d+\. (.*$)', r'<li>\1</li>', html, flags=re.MULTILINE)
        html = re.sub(r'^- (.*$)', r'<li>\1</li>', html, flags=re.MULTILINE)
        
        # 处理段落
        html = re.sub(r'\n\n([^<].*?)\n\n', r'<p>\1</p>', html, flags=re.DOTALL)
        
        # 处理Mermaid图表
        html = re.sub(
            r'```mermaid\n(.*?)\n```',
            r'<div class="mermaid">\1</div>',
            html,
            flags=re.DOTALL
        )
        
        # 处理换行
        html = html.replace('\n', '<br>')
        
        return html
    
    def _wrap_with_template(self, content: str) -> str:
        """包装HTML模板"""
        return f"""
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rust知识解释</title>
    <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
    <style>
        {self.css_styles}
    </style>
</head>
<body>
    <div class="container">
        <div class="content">
            {content}
        </div>
    </div>
    <script>
        mermaid.initialize({{ startOnLoad: true }});
        Prism.highlightAll();
    </script>
</body>
</html>
"""
    
    def _get_css_styles(self) -> str:
        """获取CSS样式"""
        return """
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f8f9fa;
        }
        
        .container {
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .content {
            padding: 30px;
        }
        
        h1, h2, h3 {
            color: #2c3e50;
            margin-top: 30px;
            margin-bottom: 15px;
        }
        
        h1 {
            font-size: 2em;
            border-bottom: 3px solid #3498db;
            padding-bottom: 10px;
        }
        
        h2 {
            font-size: 1.5em;
            border-bottom: 2px solid #ecf0f1;
            padding-bottom: 8px;
        }
        
        h3 {
            font-size: 1.2em;
            color: #34495e;
        }
        
        p {
            margin-bottom: 15px;
            text-align: justify;
        }
        
        .code-block {
            background: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
            overflow-x: auto;
            font-family: 'Fira Code', 'Monaco', 'Consolas', monospace;
            font-size: 14px;
            line-height: 1.4;
        }
        
        .inline-code {
            background: #f1f3f4;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Fira Code', monospace;
            font-size: 0.9em;
            color: #d73a49;
        }
        
        .mermaid {
            text-align: center;
            margin: 20px 0;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 6px;
        }
        
        li {
            margin-bottom: 8px;
        }
        
        strong {
            color: #2c3e50;
            font-weight: 600;
        }
        
        em {
            color: #7f8c8d;
            font-style: italic;
        }
        
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }
            
            .content {
                padding: 20px;
            }
            
            h1 {
                font-size: 1.5em;
            }
            
            h2 {
                font-size: 1.3em;
            }
        }
        """
    
    def save_html(self, markdown_content: str, file_path: str):
        """保存渲染后的HTML到文件"""
        html_content = self.render(markdown_content)
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(html_content) 